package com.yc.fruitmarket.biz.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

import com.yc.fruitmarket.biz.pvalBiz;
import com.yc.fruitmarket.dao.DBHelper;

public class pvalBizImpl implements pvalBiz {
	private DBHelper db = new DBHelper();

	@Override
	public List<Map<String, String>> getPval(String pid) {
		String sql = "select user.pic as pic, uname, starnum, valtext,pvaltime from user,product,pval where product.pid = pval.pid and pval.uid = user.uid and product.pid = ? order by pvaltime desc ";
		return db.find(sql, pid);
	}

	@Override
	public int sendPval(String uid, String pid, String pval, String rating,String oid) {
		String findtime = "select time from `order` where oid = ?";
		String timestr = db.find(findtime, oid).get(0).get("time");
		 LocalDateTime time = LocalDateTime.parse(timestr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));  
		 LocalDateTime now = LocalDateTime.now();
		 int reslut1 = 0;
		 int reslut2 = 0;
		if(time.plusDays(7).isAfter(now)) {
			String update = "update `order` set `order`.status = 5 where `order`.oid = ?";
			 reslut1 = db.doUpdate(update, oid);
			String sql = "insert into pval values(null,?,?,now(),?,?,1)";
			 reslut2 = db.doUpdate(sql, uid,rating,pval,pid);
		}else {
			String update = "update `order` set `order`.status = 3 where `order`.oid = ?";
			 reslut1 = db.doUpdate(update, oid);
			String sql = "insert into pval values(null,?,?,now(),?,?,1)";
			 reslut2 = db.doUpdate(sql, uid,rating,pval,pid);
		}
		if(reslut1 > 0 && reslut2 > 0) {
			return 1;
		}else {
			return -1;
		}
	}
	
	//获取商品评论
	@Override
	public List<Map<String, String>> getassess() {
		String sql = "select pname,pvid,a.uid,starnum,pvaltime,valtext,a.pid,a.status,uname from product,(select pvid,pval.uid,starnum,pvaltime,valtext,pid,pval.status,uname from pval,user where pval.uid=`user`.uid)as a where product.pid=a.pid";
		return db.find(sql);
	}
	
	//条件查询商品评论信息
	@Override
	public List<Map<String, String>> byassess(String pname, String pid, String starnum, String oid, String start, String end, String currentPage4, String pagesize) {
		String sql = "select pname,pvid,a.uid,starnum,pvaltime,valtext,a.pid,a.status,uname from product,(select pvid,pval.uid,starnum,pvaltime,valtext,pid,pval.status,uname from pval,user where pval.uid=`user`.uid)as a where product.pid=a.pid";
		if(!"".equals(pname) && pname != null) {
			sql += " and pname like '%"+pname+"%'";
		}
		if(!"".equals(pid) && pid != null) {
			sql += " and a.pid = " + pid;
		}
		if(!"".equals(starnum) && starnum != null) {
			if(!"0".equals(starnum)) {
				sql += " and starnum = " + starnum;
			}
		}
		if(!"".equals(oid) && oid != null) {
			sql += " and oid = " + oid;
		}
		if(!"".equals(start)) {
			start = start.substring(0, start.indexOf(" ")) + " 00:00:00";
			end = end.substring(0, end.indexOf(" ")) + " 00:00:00";
			//一天
			if(start.equals(end)) {
				start = start.substring(0, start.indexOf(" "));
				sql += " and pvaltime like '"+start+"%'";
			}else {
				//多天
				sql += " and pvaltime  between '"+start+"' and '"+end+"'";
			}
		}
		List<Map<String, String>> list = db.find(sql);
		String a = String.valueOf(list.size());
		 sql += " limit ?,?";
		 List<Map<String, String>> list1 = db.find(sql, (Integer.parseInt(currentPage4)-1) * Integer.parseInt(pagesize), Integer.parseInt(pagesize));
		 for(Map<String, String> map : list1) {
				map.put("count", a);
			}
		return list1;
	}
	
	//根据商品id求平均星数
	@Override
	public double getstarL(String pid) {
		String sql = "select * from pval where pid = " + pid;
		List<Map<String, String>> list = db.find(sql);
		double a = 0;
		int size = list.size();
		double count = 0;
		if(list.size() > 0) {
			for(Map<String, String> map : list) {
				count += Double.parseDouble(map.get("starnum"));
			}
			a = count / size;
		}
		return a;
	}
	
}
